<template>
  <div class="app-container">
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="描述">
              <span>{{ props.row.summary }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column align="center" label='ID' width="95">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>
      <el-table-column label="标题">
        <template slot-scope="scope">
          {{scope.row.title}}
        </template>
      </el-table-column>
      <el-table-column label="作者" align="center">
        <template slot-scope="scope">
          {{scope.row.author.userName}}
        </template>
      </el-table-column>
      <el-table-column label="权限" align="center">
        <template slot-scope="scope">
          {{scope.row.privacy| privacyFilter}}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{scope.row.createAt}}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" align="center">
        <template slot-scope="scope">
          {{scope.row.status| statusFilter}}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center"  width="400">
        <template slot-scope="scope">
          <el-button type="info" size="mini" icon="el-icon-edit" @click="editRow(scope.row)">修改</el-button>
          <el-button type="warning" size="mini" icon="el-icon-delete" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="page.currentPage"
        :total="page.total"
        @current-change="fetchData">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import { getList, deleteRow } from '@/api/post'

  export default {
    data() {
      return {
        list: null,
        listLoading: true,
        dialogFormVisible: false,
        formLabelWidth: '120px',
        isMod: false,
        page: {
          total: 0,
          currentPage: 1
        }
      }
    },
    filters: {
      privacyFilter(status) {
        const statusMap = {
          0: '公开',
          1: '私密'
        }
        return statusMap[status]
      },
      statusFilter(status) {
        const statusMap = {
          0: '启用',
          1: '禁用'
        }
        return statusMap[status]
      }
    },
    created() {
      this.fetchData(1)
    },
    methods: {
      fetchData(pageNo) {
        this.listLoading = true
        getList({ 'pageNumber': pageNo }).then(response => {
          this.list = response.data.content
          this.listLoading = false
          this.page.total = response.data.totalElements
        })
      },
      deleteRow(row) {
        this.listLoading = true
        deleteRow(row.id).then(response => {
          if (response.code === 0) {
            this.fetchData(this.page.currentPage)
            this.listLoading = false
          }
        })
      },
      editRow(row) {
        this.$router.push({ name: 'PostForm', params: { 'row': row }})
      }
    }
  }
</script>
